/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "ui_realmlist.h"
#include "../common.h"
#include <boost/scoped_ptr.hpp>
typedef ::boost::scoped_ptr<::node::rdb::ResultSet> ResultSetPtr;


UIRealmList::UIRealmList(void)
: realms_()
, update_lock_()
{

}

UIRealmList::~UIRealmList(void)
{

}

void UIRealmList::Update(void)
{
    ::node::sys::LockGuard<::node::sys::Mutex> guard(update_lock_);

    realms_.clear();
    ResultSetPtr result(UIDB->Query("SELECT * FROM realmlist;"));
    if (result)
    {
        do
        {
            struct Realm realm = { 0 };
            realm.id = (*result)[0]->GetUInt32();
            realm.name = (*result)[1]->GetString();
            realm.address.append((*result)[2]->GetCString())
                .append(":").append((*result)[3]->GetCString());
            realm.icon = (*result)[4]->GetUInt8();
            realm.realmflags = (eRealmFlags)(*result)[5]->GetUInt8();
            realm.timezone = (*result)[6]->GetUInt8();
            realm.populationLevel = (*result)[8]->GetFloat();
            realms_.push_back(realm);
        } while (result->NextRow());
    }

    UILOG->info("[UIRealmList::Update] Load %d realms from database", realms_.size());
}